
(function() {


	var DrawSelect = {
		name: 'Select mode',
		init: function() {
			this._emGridBox.style.cursor = 'default';
			this._emCursorBox.style.display = 'none';
			this.dragObj = null;
		},
		onmousedown: function(evt, p) {
			var obj = this.find_event_obj(evt);
			if (obj) {
				obj.setAttribute('class', 'fly');
				this._dragObj = obj;
				this._dragStart = p;
				this._dragObjOrig = {
					x: parseInt(this._dragObj.getAttribute('x')),
					y: parseInt(this._dragObj.getAttribute('y'))
				}
				this._dragObj.onmouseup = function() {
					
				}
			}
		},
		onmouseup: function(evt, p) {
			if (this._dragObj) {
				this._dragObj.removeAttribute('class');
				this._dragObj = null;
			}
		},
		onmousemove: function(evt, p) {
			if (this._dragObj) {
				var o  = this._dragObjOrig;
				var dx = p.x - this._dragStart.x;
				var dy = p.y - this._dragStart.y;
				this._dragObj.setAttribute('x', o.x + dx);
				this._dragObj.setAttribute('y', o.y + dy);
			}
		},
		onclick: function(evt) {
		},
		deinit: function() {
			this._emGridBox.style.cursor = 'none';
			this._emCursorBox.style.display = 'block';
		}
	}

	/*
	 *
	 */
	var DrawLine = {
		name: 'Draw line',
		init: function() { 
			this._drawContext = null;
		},
		onmousedown: function(ev, p) {
			this._drawContext = {
				x1: p.x,
				y1: p.y,
				x2: p.x,
				y2: p.y,
				line: null,
			}
		},
		onmousemove: function(ev, p) {
			if (!this._drawContext)	return;
			var x2 = p.x;
			var y2 = p.y;
			var x1 = this._drawContext.x1;
			var y1 = this._drawContext.y1;
			this._drawContext.x2 = x2;
			this._drawContext.y2 = y2;

			if (!this._drawContext.line) {
				var line = this._create_element('line', {x1: x1, y1: y1, x2: x2, y2: y2, class: 'fly'});
				this._drawBox.em.appendChild(line);
				this._drawContext.line = line;
			} else {
				this._drawContext.line.setAttribute('x2', x2);
				this._drawContext.line.setAttribute('y2', y2);
			}
		},
		onmouseup: function(ev) {
				this._drawContext = null;
		},
		onclick: function(ev) {
		}
	}

	var DrawLineClick = {
		name: 'Draw line on click',
		init: function() { 
			this._drawContext = null;
		},
		onclick: function(ev, p) {
			this.fly_line(p);
			if (!this._drawContext) {
				this._drawContext = {
					start: p,
					end: null
				}
			} else {
				this._drawContext.end = p;
				this.fly_end(p);
				this.add_shape('line', {
					x1: this._drawContext.start.x,
					y1: this._drawContext.start.y,
					x2: p.x,
					y2: p.y
				});
				this._drawContext = null;
			}
		},
		onmousemove: function(ev, p) {
			if (!this._drawContext)	return;

			this.fly_line(p);
		},
		onmouseup: function(ev) { },
		onmousedown: function(ev) { }
	}

	var DrawRect = {
		name: 'Draw Rectangle',
		init: function() {
			this._drawContext = null;
		},
		onclick: function(evt, p) {
			this.fly_rect(p);
			if (this._drawContext == null) {
				this._drawContext = { start: p, end: null };
			} else {
				this._drawContext.end = p;
				rect = this.clone_fly_shape();
				this.add_shape(rect);
				this.fly_end(p);
				this._drawContext = null;
			}
		},
		onmousemove: function(evt, p) {
			if (this._drawContext) {
				this.fly_rect(p);
			}
		}
	}


	var DrawPolyline = {
		name: 'Draw Polyline',
		init: function() { 
			this._drawContext = [];
		},
		onclick: function(ev, p) {
			this.fly_line(p);
			this.fly_end();
			this.fly_start_line(p);
			this._drawContext.push(p);
		},
		onmousemove: function(ev, p) {
			if (this._drawContext.length > 0) {
				this.fly_line(p);
			}
		},
		onmouseup: function(ev) { },
		onmousedown: function(ev) { },
		deinit: function() {
		}
	}


	G.SVG.Board.prototype._DrawMode = {
		select: DrawSelect,
		line: DrawLine,
		rect: DrawRect,
		lineclick: DrawLineClick,
		pathclick: DrawPolyline,


	}

 })();
